from socket import *

try:
    server_socket = socket(AF_INET, SOCK_STREAM)
    server_socket.bind(("0.0.0.0", 5000))
    server_socket.listen()
    sock, addr = server_socket.accept()
    data = b""
    while True:
        packet = sock.recv(1024)
        if not packet:
            break
        data += packet
        print(f"Received data from client: {data.decode()}")
    sock.send(f"{data.decode()} from server".encode("utf-8"))
except Exception as e:
    print(f"client close: {e}")
finally:
    sock.close()
    server_socket.close()
